package com.inctech.controller.runFlow;

import javax.annotation.Resource;

import net.sf.json.JSONObject;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.inctech.flowengine.FlowWebservice;
import com.inctech.interceptor.AuthPassport;
import com.inctech.service.IFlowEngineService;

@Controller
@RequestMapping("/runflow")
public class RunFlowController {
	final Logger logger = LoggerFactory.getLogger(RunFlowController.class);
	
	@Resource(name="flowEngineService")
	private IFlowEngineService flow;
	private FlowWebservice getClient() {
		return flow.getFlowClinet();
	}
	
	@AuthPassport
	@RequestMapping("/runindex")
	public String index() {
		return "runflow/runindex";
	}
	
	@ResponseBody
	@RequestMapping("/createuser")
	public String createUser(String id, String name, String email, String pwd) {
		JSONObject map = new JSONObject();
		map.put("id", id);
		map.put("name", name);
		map.put("email", email);
		map.put("pwd", pwd);
		String status = this.getClient().createUser(map.toString());
		return status;
	}
	
	@ResponseBody
	@RequestMapping("/creategroup")
	public String createGroup(String id, String name, String type) {
		JSONObject map = new JSONObject();
		map.put("id", id);
		map.put("name", name);
		map.put("type", type);
		String status = this.getClient().createGroup(map.toString());
		return status;
	}
	
	@ResponseBody
	@RequestMapping("/createmembership")
	public String createMembership(String userId, String groupId) {
		String status = this.getClient().createMembership(userId, groupId);
		return status;
	}
	
	@ResponseBody
	@RequestMapping("/deleteuser")
	public String deleteUser(String userId) {
		String status = this.getClient().deleteUser(userId);
		return status;
	}
	
	@ResponseBody
	@RequestMapping("/deletegroup")
	public String deleteGroup(String groupId) {
		String status = this.getClient().deleteGroup(groupId);
		return status;
	}
	
	@ResponseBody
	@RequestMapping("/deletemembership")
	public String deleteMembership(String userId, String groupId) {
		String status = this.getClient().deleteMembership(userId, groupId);
		return status;
	}
}
